﻿using System;
using System.Globalization;
using System.IO;

using iStudio.Structure.Engine;

namespace iStudio.Control.Audio
{
    /// <summary>
    /// Interaction logic for ProjectControl.xaml
    /// </summary>
    public partial class ProjectControl
    {
        public ProjectControl()
        {
            InitializeComponent();
        }


        public void RefreshUi()
        {
            // Legacyc
            //foreach (FunctionEngine function in _functionWindows)
            //{
            //if (function.IsLoaded)
            //{
            //    function.RefreshData ();
            //}
            //else
            //{
            //    _functionWindows.Remove (function);
            //}
            //}

            //
            // Setup the channel user interface for stereo or mono channels
            //

            // Modify the user interface if channel count is one
            //if ((AudioEngine.Instance.ChannelCount == 1) && (grid_channel.RowDefinitions.Count == 2))
            //{
            //    grid_channel.RowDefinitions.RemoveAt (1);
            //    grid_channel.ColumnDefinitions[0].Width = new GridLength (0);
            //}
            //    // Add back the stereo channel
            //else if ((AudioEngine.Instance.ChannelCount == 2) && (grid_channel.RowDefinitions.Count == 1))
            //{
            //    RowDefinition rd = new RowDefinition
            //                           {
            //                               MinHeight = 70
            //                           };
            //    grid_channel.RowDefinitions.Add (rd);
            //    grid_channel.ColumnDefinitions[0].Width = new GridLength (30);
            //}

            switch (AudioEngine.Instance.Project.Format.Channels)
            {
                case 8:
                    file_channels.Text = "7.1 - Surround";

                    break;

                case 6:
                    file_channels.Text = "5.1 - Surround";

                    break;
                case 2:
                    file_channels.Text = "2 - Stereo";

                    break;
                case 1:
                    file_channels.Text = "1 - Mono";

                    break;
            }

            //
            // Display file information
            //

            // Get file location
            file_location.Text = String.IsNullOrEmpty(AudioEngine.Instance.Project.Name) ? "<file not saved>" : Path.GetFullPath(AudioEngine.Instance.Project.Name);

            if (file_location.Text != "<file not saved>")
            {
                file_location.Text = Path.GetFileName(file_location.Text);
            }

            file_sample_rate.Text = AudioEngine.Instance.Project.Format.SampleRate + " Hz";
            file_sample_size.Text = AudioEngine.Instance.Project.Format.BitsPerSample + " bit";

            //
            // Construct file length
            //

            // Minutes
            string soundLength = Math.Floor(AudioEngine.Instance.ChannelLength / 60).ToString(CultureInfo.InvariantCulture);

            if (soundLength.Length < 2)
            {
                soundLength = "0" + soundLength;
            }

            soundLength += ":";

            double seconds = AudioEngine.Instance.ChannelLength - (Math.Floor(AudioEngine.Instance.ChannelLength / 60) * 60);
            seconds = Math.Ceiling(seconds);

            if (seconds < 10)
            {
                soundLength += "0" + Convert.ToInt32(seconds).ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                soundLength += Convert.ToInt32(seconds).ToString(CultureInfo.InvariantCulture);
            }

            file_length.Text = soundLength;
        }
    }
}
